package guoanmaker.operator.business.controller;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import guoanmaker.operator.business.model.InRoundPhone;
import guoanmaker.operator.business.model.OperatorPatent;
import guoanmaker.operator.business.service.OperatorPatentServiceInterface;
import guoanmaker.operator.helper.common.AjaxJson;
import guoanmaker.operator.helper.common.abstractobj.ApiController;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping(ApiController.OPERATOR_PATENT)
public class OperatorPatentController {
	@Autowired
	private OperatorPatentServiceInterface operatorPatentServiceInterface;
	/**
	 * type app传1 pc传2
	* @Title: saveHotServer 
	* @Description: TODO(这里用一句话描述这个方法的作用) 
	* @param inRoundPhone
	* @return    设定文件 
	* @return AjaxJson    返回类型 
	* @throws 
	 */
	 @ApiOperation(value = "保存专利服务商品", notes = "新增/修改专利服务商品")
	    @PostMapping(value = "saveOperatorPatent")
	    public AjaxJson saveHotServer(@RequestBody InRoundPhone inRoundPhone) {
		 AjaxJson ajaxJson=new AjaxJson();
	    	try{
	    		OperatorPatent[] operatorPatents=inRoundPhone.getOperatorPatents();
	    		operatorPatentServiceInterface.delectAllBycitycode(inRoundPhone.getCityId(),operatorPatents[0].getType());
	    	for(OperatorPatent patent:operatorPatents){
	    		/*if(null!=hotserver.getId()&&!hotserver.getId().equals("")){
	    			operatorHotServerServiceInterface.deleteHotServerById(hotserver.getId());
	    		}*/
	    		patent.setCitycode(inRoundPhone.getCityId());
	    		patent.setCreateDate(new Date());
	    		if (patent.getCommodityNum()!= null && ! "".equals(patent.getCommodityNum())) {
	    			operatorPatentServiceInterface.saveOperatorPatent(patent);
	    		} else if (patent.getType()==1 && "".equals(patent.getCommodityNum())){
	    			operatorPatentServiceInterface.saveOperatorPatent(patent);
	    		}
	    		
	    	}
	    	ajaxJson.setSuccess(true).setStatus(200).setData(null);
	    	}catch(Exception e){
	    		e.printStackTrace();
	    		ajaxJson.setSuccess(false).setStatus(250).setData(null);
	    	}
	        return ajaxJson;
	    }
	 
	 @ApiOperation(value = "删除专利服务商品", notes = "根据Id删除专利服务商品")
	    @DeleteMapping(value = "deleteOperatorPatent")
	    public AjaxJson deleteOperatorPatent(String id) {
		 AjaxJson ajaxJson=new AjaxJson();
		 operatorPatentServiceInterface.deleteOperatorPatent(id);
	        return ajaxJson.setSuccess(true).setStatus(200).setData("数据删除成功!");
	    }
	 /**
	  * type app传1 pc传2
	 * @Title: getAllOperatorTrademark 
	 * @Description: TODO(这里用一句话描述这个方法的作用) 
	 * @param cityId
	 * @param type
	 * @return    设定文件 
	 * @return AjaxJson    返回类型 
	 * @throws 
	  */
	  @ApiOperation(value = "根据code查询所有专利服务商品", notes = "根据code查询所有专利服务商品")
	    @PostMapping(value = "getAllOperatorPatent")
	    public AjaxJson getAllOperatorPatent(String cityId,int type) {
		  AjaxJson ajaxJson=new AjaxJson();
	    	try{
	    	List<OperatorPatent> operatorPatents=operatorPatentServiceInterface.selectOperatorPatent(cityId, type);
	    	
	    		ajaxJson.setSuccess(true).setStatus(200).setData(operatorPatents);
	    	}catch(Exception e){
	    		ajaxJson.setSuccess(true).setStatus(250).setData(null);
	    	}
	        return ajaxJson;
	    }
	  /**
	   * 
	      * @Title: selectOperatorPatentByCommodityNum  
	      * @Description: 根据商品编号 城市编码 查询商品详情  
	      * @param @param commodityNum
	      * @param @param cityCode
	      * @return AjaxJson      
	      * @throws
	   */
	  @ApiOperation(value = "查询商品", notes = "查询商品")
	  @PostMapping("selectOperatorPatentByCommodityNum")
	  public AjaxJson selectOperatorPatentByCommodityNum(Integer commodityNum, String cityCode) {
		  AjaxJson ajaxJson=new AjaxJson();
		  try {
			  ajaxJson.setSuccess(true).setStatus(200).setData(operatorPatentServiceInterface.selectOperatorPatentByCommodityNum(commodityNum, cityCode)).setMsg("操作成功");
		} catch (Exception e) {
			ajaxJson.setSuccess(false).setStatus(500).setData(null).setMsg("操作失败");
		}
		  return ajaxJson;
	  }
	  
	  @ApiOperation(value = "Pc app返回商品数据", notes = "app返回商品数据")
	  @PostMapping("getAlltOperatorPatentCommodity")
	  public AjaxJson getAlltOperatorPatentCommodity(String cityCode,int type) {
		  AjaxJson ajaxJson=new AjaxJson();
		  try {
			  ajaxJson.setSuccess(true).setStatus(200).setData(operatorPatentServiceInterface.getAlltOperatorPatentCommodity(cityCode,type)).setMsg("操作成功");
		} catch (Exception e) {
			e.printStackTrace();
			ajaxJson.setSuccess(false).setStatus(500).setData(null).setMsg("操作失败");
		}
		  return ajaxJson;
	  }
}
